﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessObject;
using Utility;
using System.Resources;
namespace WebTeamplate
{
    public partial class Admin_BangDonViTinh : BasePage
    {
        int count = 0;
        #region Init,Load
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientDefaultButton.SetPageDefaultButton(this.Page, btnSave.ClientID);
            if (!Page.IsPostBack)
            {

                CommonFunction.LoadSubCate(drlLoaiHangHoa, CommonFunction.DEFAULT_LOAIHANGHOA_CODE, false);
                drlLoaiHangHoa.SelectedValue = CommonFunction.GetValueFromQueryString(this.Page, "loaihh");
                LoadDVTData();
                LoadData();
                drlLoaiHangHoa.Focus();
            }
        }

        private void LoadDVTData()
        {
            List<SubCategoryResult> lstCate = SubCategoryUtil.GetAllByCategory(CommonFunction.DEFAULT_UNIT_CODE);
            if (lstCate != null && lstCate.Count > 0)
            {
                foreach (SubCategoryResult objDVT in lstCate)
                {
                    cblDonViTinh.Items.Add(new ListItem("&nbsp;&nbsp;&nbsp;" + objDVT.SubCategoryName, objDVT.SubCategoryId.ToString()));
                }
            }


        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitTextControl();
        }
        #endregion

        #region Methods

        public void InitTextControl()
        {
            btnSave.Text = Resources.Resource.LABEL_BUTTON_SAVE;
            btnDelete.Text = Resources.Resource.LABEL_DELETE;
            btnCancel.Text = Resources.Resource.LABEL_BUTTON_CANCEL;
            vsumCategory.ValidationGroup = refvLoaiHangHoa.ValidationGroup = refvDVT.ValidationGroup = btnSave.ValidationGroup;
            refvLoaiHangHoa.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblLoaiHangHoa));
            refvDVT.ErrorMessage = string.Format(Resources.Resource.MESSAGE_REQUIRE_INPUT, CommonFunction.GetTextLabel(lblDVT));
        }

        public void LoadData()
        {
            hidListId.Value = string.Empty;
            hidListIdSelect.Value = string.Empty;
            List<SubCategoryResult> lstResult = SubCategoryUtil.GetDonViTinhByLoaiHangHoa(ConvertValueObject.ParseInt(drlLoaiHangHoa.SelectedValue));
            if (lstResult != null && lstResult.Count > 0)
                count = lstResult.Count;
            rptData.DataSource = lstResult;
            rptData.DataBind();
        }
        private void Delete(string id)
        {
            if (DonViTinhLoaiHangHoaUtil.Delete(ConvertValueObject.ParseInt(drlLoaiHangHoa.SelectedValue), ConvertValueObject.ParseInt(id)))
                LoadData();

        }



        #endregion

        #region Events,Handler

        protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                SubCategoryResult cate = (SubCategoryResult)e.Item.DataItem;
                if (cate == null)
                    return;
                Literal ltr = (Literal)e.Item.FindControl("ltrNo");
                ltr.Text = (e.Item.ItemIndex + 1).ToString();

                ltr = (Literal)e.Item.FindControl("ltrSubName");
                ltr.Text = Server.HtmlEncode(cate.SubCategoryName);
                ImageButton imgButton = (ImageButton)e.Item.FindControl("btnPublish");
                if (cate.IsPublished)
                    imgButton.ImageUrl = CommonFunction.DEFAULT_ACTIVE_IMAGEURL;
                else
                    imgButton.ImageUrl = CommonFunction.DEFAULT_DEACTIVE_IMAGEURL;
                imgButton.Enabled = false;
                imgButton = (ImageButton)e.Item.FindControl("imgOrderUp");
                if (e.Item.ItemIndex + 1 == count)
                    imgButton.Visible = false;
                else
                {
                    imgButton.ToolTip = Resources.Resource.TOOLTIP_ORDER_UP;
                    imgButton.CommandArgument = cate.SubCategoryId.ToString();
                    imgButton.ImageUrl = CommonFunction.DEFAULT_ORDERUP_IMAGEURL;
                }
                imgButton.Enabled = true;
                imgButton = (ImageButton)e.Item.FindControl("imgOrderDown");
                if (e.Item.ItemIndex == 0)
                    imgButton.Visible = false;
                else
                {
                    imgButton.CommandArgument = cate.SubCategoryId.ToString();
                    imgButton.ToolTip = Resources.Resource.TOOLTIP_ORDER_DOWN;
                    imgButton.ImageUrl = CommonFunction.DEFAULT_ORDERDOWN_IMAGEURL;
                    imgButton.Visible = true;
                }
                LinkButton lbtnDelete = (LinkButton)e.Item.FindControl("lbtnDelete");
                lbtnDelete.Text = Resources.Resource.LABEL_DELETE;
                lbtnDelete.CommandArgument = cate.SubCategoryId.ToString();
                hidListId.Value += cate.SubCategoryId + ";";
            }
        }
        protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            switch (e.CommandName)
            {

                case "delete":
                    {
                        Delete(id);
                        break;
                    }
                case "OrderUp":
                    {
                        OrderUp(id);
                        break;
                    }
                case "OrderDown":
                    {
                        OrderDown(id);
                        break;
                    }
            }

        }

        private void OrderDown(string id)
        {
            DonViTinhLoaiHangHoa obj = new DonViTinhLoaiHangHoa();
            obj.MaDonViTinh = ConvertValueObject.ParseInt(id);
            obj.MaLoaiHangHoa = ConvertValueObject.ParseInt(drlLoaiHangHoa.SelectedValue);
            if (DonViTinhLoaiHangHoaUtil.Order(obj, CommonFunction.DEFAULT_ORDER_DOWN))
                LoadData();
        }

        private void OrderUp(string id)
        {
            DonViTinhLoaiHangHoa obj = new DonViTinhLoaiHangHoa();
            obj.MaDonViTinh = ConvertValueObject.ParseInt(id);
            obj.MaLoaiHangHoa = ConvertValueObject.ParseInt(drlLoaiHangHoa.SelectedValue);
            if (DonViTinhLoaiHangHoaUtil.Order(obj, CommonFunction.DEFAULT_ORDER_UP))
                LoadData();
        }



        protected void btnDelete_Click(object sender, EventArgs e)
        {
            List<DonViTinhLoaiHangHoa> lstCate = new List<DonViTinhLoaiHangHoa>();
            string[] arr = hidListIdSelect.Value.Split(';');
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i].ToString() != string.Empty)
                {
                    DonViTinhLoaiHangHoa obj = new DonViTinhLoaiHangHoa();
                    obj.MaDonViTinh = ConvertValueObject.ParseInt(arr[i].ToString());
                    obj.MaLoaiHangHoa = ConvertValueObject.ParseInt(drlLoaiHangHoa.SelectedValue);
                    lstCate.Add(obj);
                }
            }
            DonViTinhLoaiHangHoaUtil.Delete(lstCate);
            LoadData();
        }
        #endregion
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                List<DonViTinhLoaiHangHoa> lstResult = new List<DonViTinhLoaiHangHoa>();
                foreach (ListItem item in cblDonViTinh.Items)
                {
                    if (item.Selected)
                    {
                        DonViTinhLoaiHangHoa objData = new DonViTinhLoaiHangHoa();
                        objData.MaDonViTinh = ConvertValueObject.ParseInt(item.Value);
                        objData.MaLoaiHangHoa = ConvertValueObject.ParseInt(drlLoaiHangHoa.SelectedValue);
                        lstResult.Add(objData);
                    }
                }
                if (lstResult.Count > 0)
                    DonViTinhLoaiHangHoaUtil.Insert(lstResult);
                Response.Redirect("LoaiHangHoa.aspx");
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("LoaiHangHoa.aspx");
        }
        protected void drlLoaiHangHoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
